package algorithm;

import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;

public class leetcode_0148 {
    public ListNode sortList(ListNode head) {
        List<Integer> list=new LinkedList<>();
        ListNode p=head;
        if(head==null) return head;
        while(p!=null){
            list.add(p.val);
            p=p.next;
        }
        list.sort(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1-o2;
            }
        });
        p=head;
        int count=0;
        while(p!=null){
            p.val=list.get(count);
            p=p.next;
            count++;
        }
        return head;
    }
}
